import 'package:flutter_09_game/base/controller/base_list_more_controller.dart';
import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_09_game/http/http_export.dart';
import 'package:flutter_09_game/pages/deposit/models/model_record_recharge.dart';
import 'package:flutter_09_game/pages/deposit/utils_deposit.dart';
import 'package:flutter_date_pickers/flutter_date_pickers.dart';
import 'package:get/get.dart';

class DepositRecordWithdrawLogic extends BaseListMoreController {
  RxString selectorTagRx = LocaleKeys.withdrawAll.tr.obs;

  RxString symbolRx = ''.obs;

  // 0 全部  3 成功   1 等待到账 4 失败
  RxString statusRx = LocaleKeys.allStatus.tr.obs;
  RxString timeRx = LocaleKeys.today.tr.obs;

  // 日期范围相关属性
  Rx<DatePeriod?> selectedDateRange = Rx<DatePeriod?>(null);

  int getStatus() {
    if (statusRx.value == LocaleKeys.withdrawSuccess.tr) {
      return 3;
    } else if (statusRx.value == LocaleKeys.waitingForArrival.tr) {
      return 1;
    } else if (statusRx.value == LocaleKeys.withdrawFailed.tr) {
      return 4;
    }
    return 0;
  }

  @override
  Future<List> loadData() async {
    final dateRange = UtilsDeposit.calculateDateRange(timeRx.value,
        selectedDateRange: selectedDateRange.value);
    // TODO: implement loadData
    HttpResponse response = await HttpManager().get(API.getWithdraw,
        params: {
          'page': pageNum,
          "size": pageSize,
          'symbol': symbolRx.value,
          'status': getStatus(),
          'type': selectorTagRx.value == LocaleKeys.withdrawAll.tr ? 1 : 2,
          'start': dateRange['start'],
          'end': dateRange['end'],
        },
        fromJson: ModelRecordRecharge.fromJson);
    // if (response.isSuccess) {

    return response.data ?? [];
    // }
    // response []

    // return [1];
  }

  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();
  }

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();

    everAll([symbolRx, statusRx,selectorTagRx], (value) {
      // refreshList();
      refreshController.requestRefresh();
    });
  }

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
  }

  /// 更新时间选择
  void updateTimeSelection(String timeType) {
    timeRx.value = timeType;
    if (timeType == LocaleKeys.custom.tr) {
      // showDateRangePicker();
      UtilsDeposit.showDateRangePicker(selectedDateRange.value).then((value) {
        if (value is DatePeriod) {
          selectedDateRange.value = value;
          refreshController.requestRefresh();
          //     timeRx.value = LocaleKeys.custom.tr;
          //     // 重新加载数据
          //     refreshList();
        }
      });
    } else {
      // 清除自定义日期范围
      selectedDateRange.value = null;
      refreshController.requestRefresh();
    }
  }
}
